<%-- 
    Document   : index
    Created on : Apr 7, 2011, 2:04:48 PM
    Author     : Robert Gunawan
--%>

<%@page import="java.util.ArrayList"%>
<%@page import="jesstalk.Db"%>
<%@page import="java.lang.Integer"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    // cek login or not
    if(session.getAttribute("username") != null) {
        response.sendRedirect("chat.jsp");
    }

    //===================================AREA REGISTRAST=============================
    boolean cek = true;
    String username = "";
    String password = "";
    String name = "";
    String email = "";
    //brutal cek --" ga ada isset
    if(request.getParameter("username") == null)
        {
        cek = false;
        }
    else
        username = request.getParameter("username");
    if(request.getParameter("name") == null)
        {
        cek = false;
        }
    else
        name = request.getParameter("name");
    if(request.getParameter("password") == null)
        {
        cek = false;
        }
    else
        password = request.getParameter("password");
    if(request.getParameter("email") == null)
        {
        cek = false;
        }
    else
        email = request.getParameter("email");
    //cek variable cek apakah true apa kaga
    if(cek == true) //jika true
        {
        Db Regis = Db.get(getServletContext());
        String query = "insert into user (username,password,name,email) values ('"+username+"','"+password+"','"+name+"','"+email+"')";
        //cek apakah username sudah ada
        ArrayList<String> CekUsername = new ArrayList<String>();
        CekUsername = Regis.getQuery("jesstalk", "select * from user where username = '"+username+"'", "username");
        if(CekUsername.size() > 0)
            {
            %>
            <script type ="text/javascript">
                alert("Username already taken");
            </script>
            <%
            }
        else
            Regis.insertInto("jesstalk", query);
        }


    //=================================AREA LOGIN========================================
    boolean cekLogin = true;
    String usernameLoginString = "";
    String passwordLoginString = "";
    if (request.getParameter("usernameLogin") == null)
        {
            cekLogin = false;
        }
    else
        usernameLoginString = request.getParameter("usernameLogin");

    if (request.getParameter("passwordLogin") == null)
        {
            cekLogin = false;
        }
    else
        passwordLoginString = request.getParameter("passwordLogin");

    if(cekLogin == true) //berhasil memasukan parameter
        {
        //query dulu
        Db Login = Db.get(getServletContext());
        ArrayList<String> usernameArray = new ArrayList<String>();
        ArrayList<String> passwordArray = new ArrayList<String>();
        String queryUsername = "select * from user where username = '"+ usernameLoginString+"'";
        usernameArray = Login.getQuery("jesstalk", queryUsername, "username");
        if(usernameArray.size() != 1) //gagal login karena username tidak ada
            {
            %>
            <script type ="text/javascript">
                alert("Login Failed!");
            </script>
            <%
            }
        else //berhasil ketemu username
            {
            String queryPassword = "select * from user where username ='"+ usernameArray.get(0)+"'";
            passwordArray = Login.getQuery("jesstalk", queryPassword, "password");
            if (!passwordArray.get(0).equals(passwordLoginString)) //gagal login karena password tidak sesuai
                {
                    %>
                    <script type ="text/javascript">
                        alert("Login failed!");
                    </script>
                    <%
                }
            else //berhasil login
                {
                    //ubah status jadi online dulu
                    
                    //mulai redirect
                    String queryRole = "select * from user where username ='"+ usernameArray.get(0)+"'";
                    ArrayList<String> Role = new ArrayList<String>();
                    Role = Login.getQuery("jesstalk", queryRole, "isAdmin");
                    //set session
                    session.setAttribute("username",usernameArray.get(0));
                    if(Role.get(0).equals("1")) //admin
                        {
                        //out.println("admin"); //redirect ke halaman admin
                        //set session admin kategori ke 0 dan 0
                        session.setAttribute("kategoriBefore", "0");
                        session.setAttribute("kategoriAfter", "0");
                        response.sendRedirect("adminUserControl.jsp");
                        }
                    else
                        {
                        //cek ke banned ga usernya
                        String BannedQuery ="select * from user where username ='"+usernameArray.get(0)+"'";
                        ArrayList<String> IsBanned = Login.getQuery("jesstalk", BannedQuery, "isBanned");
                        //jika kebanned maka tampilkan pesan banned
                        if(IsBanned.get(0).equals("1"))
                            {
                            session.removeAttribute("username"); //unset session
                            %>
                            <script type="text/javascript">
                            alert("Username has been banned!");
                            </script>
                            <%
                            }
                        //jika ngga, maka redirect ke halaman chat
                        else
                            {
                            response.sendRedirect("chat.jsp");
                            }
                        }
                }
            }
        }

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="script/validator.js"></script>
        <link href="style/style.css" rel="stylesheet" type="text/css">
        <title>jesstalk</title>
    </head>
    <body>
    <div id="wrap">
        <div id="headerWrap">
            <div id="header">
            </div>
            <div id="headerTab">
            </div>
        </div>
        <div id="content">
            <div id ="formLogin">
                <div id="judulLogin">
                <h2>Login</h2>
                </div>
                <div id="formLogin2">
                <form action="#" method="post" id="Login" name="Login" onsubmit=''>
                    Username <span id="usernameLoginSpan"> : </span> <input type="text" id="usernameLogin" name="usernameLogin"/>
                    <br />
                    Password <span id="passwordLoginSpan"> : </span> <input type="password" id="passwordLogin" name="passwordLogin"/>
                    <br />
                    <input type="submit" id="loginSubmit" name="regisSubmit" value ="Login"/>
		</form>
                </div>
            </div>
            <div id="Border">
               
            </div>
            <div id ="formRegistrasi">
                <div id="judulRegistrasi">
                    <h1> Registration </h1>
                </div>
                <div id="formRegisDiv">
                <form action="#" method="post" id="Registrasi" name="Registrasi" onsubmit='return CekRegistrasi(username.value, password.value, password2.value, name.value, email.value, labelUsernameSalah,labelPasswordSalah,labelPassword2Salah,labelNameSalah,labelEmailSalah);'>
                    Username <span id="usernameSpan"> : </span> <input type="text" id="username" name="username"/> <label class="peringatan" id="labelUsernameSalah"> </label>
                    <br />
                    Password <span id="passwordSpan"> : </span> <input type="password" id="password" name="password"/> <label class="peringatan" id="labelPasswordSalah"> </label>
                    <br />
                    Password (retype) <span id="password2Span"> : </span> <input type="password" id="password2" name="password2"/> <label class="peringatan" id="labelPassword2Salah"> </label>
                    <br />
                    Name <span id="nameSpan"> : </span> <input type="text" id="name" name="name"/> <label class="peringatan" id="labelNameSalah"> </label>
                    <br />
                    Email <span id="emailSpan"> : </span> <input type="text" id="email" name="email"/> <label class="peringatan" id="labelEmailSalah"> </label>
                    <br />
                    <input type="submit" id="regisSubmit" name="regisSubmit" />
		</form>
                </div>
            </div>
            <div id="clear">
            </div>
        </div>
        <div id ="footer">
            <div id="isiFooter">
                <center>
                    Created for Internet Programming Assignment, Bandung Institute of Technology
                    <br/>
                    Copyright 2011
                    <br/>
                    <a href="http://www.facebook.com/sir.dip">Dimas Tri Ciputra</a>
                    <a href="http://www.facebook.com/profile.php?id=1052323993">Ecky Putrady</a>
                    <a href="http://www.facebook.com/Gunawan.Robert">Robert Gunawan</a>
                </center>
            </div>
        </div>
    </div>
    </body>
</html>
